/* 学习目标：实现清除已完成功能  */

const initialState = {
  list: [
    { id: 1, task: "学写VUe3", isDone: true },
    { id: 2, task: "学习TS", isDone: true },
    { id: 3, task: "学习React", isDone: false }
  ]
}


// 1. 扩展ActionType的类型
export type ActionType =
  | { type: "todo/delById", payload: number }
  | { type: 'todo/clearDone', }

// 🗝️🗝️ TS+Redux中， reducer函数，不要解构Action
const todoReducer = (state = initialState, action: ActionType): typeof initialState => {
  switch (action.type) {
    case 'todo/delById':
      return {
        ...state,
        list: state.list.filter((item) => item.id !== action.payload)
      }
    // 4. reducer完成计算
    case 'todo/clearDone':
      return {
        ...state,
        list: state.list.filter(item => !item.isDone)
      }
    default:
      return state
  }
}

export default todoReducer




